package com.sun.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author SunRui
 * @description 逾期罚款
 * @date 2024/7/4 21:20
 */
public class BorrowUtil {

    public float calculateFine(String endDate, String beginDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = sdf.parse(endDate);
        Date date2 = sdf.parse(beginDate);

        // 计算两个日期之间的毫秒差
        long diffInMillis = date1.getTime() - date2.getTime();
        long diffInDays = diffInMillis / (24 * 60 * 60 * 1000);

        // 如果时间差小于1天，则按1天算
        if (diffInDays < 1) {
            diffInDays = 1;
        }

        // 根据天数差计算罚款金额
        float fineAmount = diffInDays * 10.0f;

        return fineAmount;
    }

    //获取当前日期
    public String getCurrentDate() {
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化当前日期和时间
        String formattedDate = currentDateTime.format(formatter);
        return formattedDate;
    }

}
